package com.lucenten.collect.gnss.runnable;

import cn.quevo.common.lang.DateFormatUtils;
import com.lucenten.collect.gnss.entity.GnssFile;
import com.lucenten.collect.gnss.entity.SatelliteStation;
import com.lucenten.collect.gnss.handler.impl.*;
import com.lucenten.collect.gnss.service.ObsErvationService;
import com.lucenten.collect.gnss.service.SatelliteStationService;
import com.lucenten.mq.SendJmsInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.*;
import java.util.Date;
import java.util.Map;

/**
 * ZIP解压线程
 */
@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GnssUnZipRunnable implements Runnable{
    // 线程编号
    private Integer index;
    // 根目录
    private Path root;
    // 文件
    private Path file;
    // 全部站点信息(key=stationId+","+stationName)
    private Map<String, SatelliteStation> stationMap;
    // 全部站点信息(key=reStationName)
    private Map<String, SatelliteStation> reStationMap;

    private SatelliteStationService satelliteStationService;
    private ObsErvationService obsErvationService;

    @Override
    public void run() {
       init(file);
    }
    private void one(GnssFile gnssFile){
        UnZipGnssHandler unzip = new UnZipGnssHandler(satelliteStationService,obsErvationService);
        BackupGnssHandler backup = new BackupGnssHandler(satelliteStationService,obsErvationService);
        TransGnssHandler trans = new TransGnssHandler(satelliteStationService,obsErvationService);
        StorageGnssHandler storage = new StorageGnssHandler(satelliteStationService,obsErvationService);
        ReNameGnssHandler reName = new ReNameGnssHandler(satelliteStationService,obsErvationService);
        StatisticsGnssHandler statistics = new StatisticsGnssHandler(satelliteStationService,obsErvationService);
        TecpTeclGnssHandler teclTecp = new TecpTeclGnssHandler(satelliteStationService,obsErvationService);
        CleanGnssHandler clean = new CleanGnssHandler(satelliteStationService,obsErvationService);
        STecHoureGnssHandler stec = new STecHoureGnssHandler(satelliteStationService,obsErvationService);

        unzip.setChainHandler(backup);
        backup.setChainHandler(trans);
        trans.setChainHandler(storage);
        storage.setChainHandler(reName);
        reName.setChainHandler(statistics);
        statistics.setChainHandler(teclTecp);
        teclTecp.setChainHandler(stec);
        stec.setChainHandler(clean);
        unzip.process(gnssFile);
    }

    private void one2(GnssFile gnssFile){
        UnZipGnssHandler unzip = new UnZipGnssHandler(satelliteStationService,obsErvationService);
        BackupGnssHandler backup = new BackupGnssHandler(satelliteStationService,obsErvationService);
        unzip.setChainHandler(backup);
        unzip.process(gnssFile);
    }

    private void init(Path source){
        log.info("startThread init  " + source.toString());
//        SendJmsInfo.sendGnssMq(1, source.getFileName().normalize().toString(), 0);
        GnssFile gnssFile = GnssFile.getInstance();
        gnssFile.setSourceFile(source.normalize().toString());
        gnssFile.setIndex(index);
        gnssFile.setRoot(root.normalize().toString());
        gnssFile.setStations(stationMap);
        gnssFile.setReStations(reStationMap);
        try {
            one(gnssFile);
        }catch (Exception e) {
            log.error("线程启动异常",e);
        }
    }
}
